package kxfang.com.common.core.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * recycleView的Adapter,不适用dataBinding
 */
public abstract class BaseRecycleViewAdapter<T> extends RecyclerView.Adapter<BaseRecycleViewAdapter.ViewHolder> implements IAdapterMethod<T> {

    private Context context;
    private List<T> data;

    private OnClickItemViewListener<T> listener;

    public BaseRecycleViewAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;
    }

    public void setListener(OnClickItemViewListener<T> listener) {
        this.listener = listener;
    }

    public OnClickItemViewListener getListener() {
        return listener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(getViewLayout(), viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        bindView(data.get(i), viewHolder, i);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick(data.get(i), i, viewHolder);
                if (listener != null) {
                    // 点击事件回调
                    listener.onItemView(data.get(i), i, viewHolder);
                }
            }
        });
    }

    /**
     * 点击事件的处理
     * @param t
     * @param i
     * @param viewHolder
     */
    protected void itemClick(T t, int i, ViewHolder viewHolder){

    }

    @Override
    public Context getContext() {
        return context;
    }

    @Override
    public Resources getResource() {
        return context.getResources();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public List<T> getDataList() {
        return data;
    }

    @Override
    public boolean isEmpty() {
        if (data == null || data.size() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public void removeData(T object) {
        if (object == null) {
            return;
        }
        data.remove(object);
        notifyDataSetChanged();
    }

    @Override
    public void removeData(int position) {
        if (position < 0 || position > getItemCount()) {
            return;
        }
        data.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public void addData(T object) {
        if (object == null) {
            return;
        }
        data.add(object);
        notifyDataSetChanged();
    }

    @Override
    public void addData(int position, T object) {
        if (position < 0 || position > getItemCount() || object == null) {
            return;
        }
        data.add(position, object);
        notifyDataSetChanged();
    }

    @Override
    public void addAll(List<T> objects) {
        if (objects == null || objects.size() == 0) {
            return;
        }
        for (T t : objects) {
            if (data.contains(t)) {
                continue;
            }
            data.add(t);
        }
        notifyDataSetChanged();
    }

    @Override
    public void clearData() {
        if (data != null) {
            data.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public void updateData(List<T> objects) {
        if (objects == null) {
            return;
        }
        this.data = objects;
        notifyDataSetChanged();
    }

    /**
     * 获取布局文件
     *
     * @return
     */
    public abstract int getViewLayout();

    /**
     * 初始化界面值
     *
     * @param model
     * @param holder
     * @param position
     */
    public abstract void bindView(T model, ViewHolder holder, int position);

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private View itemView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.itemView = itemView;
        }

        /**
         * 根据id获取控件
         *
         * @param id
         * @return
         */
        public View findViewById(int id) {
            return itemView.findViewById(id);
        }

        /**
         * 获取图片控件
         *
         * @param id
         * @return
         */
        public ImageView getImageView(int id) {
            return (ImageView) findViewById(id);
        }

        /**
         * 获取文本控件
         *
         * @param id
         * @return
         */
        public TextView getTextView(int id) {
            return (TextView) findViewById(id);
        }
    }

    public interface OnClickItemViewListener<T> {
        void onItemView(T model, int position, ViewHolder holder);
    }
}
